package com.xyhuo.leetcode.november;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 删除并获取点数
 * https://leetcode.cn/problems/delete-and-earn
 *
 * @time 2023/11/23
 */
public class DeleteAndEarn {

//    public static void main(String[] args) {
//        DeleteAndEarn deleteAndEarn = new DeleteAndEarn();
////        int[] nums = {2, 2, 3, 3, 3, 4, 5};
//        int[] nums = {3, 1};
//        int result = deleteAndEarn.deleteAndEarn(nums);
//        System.out.println(result);
//    }

    public int deleteAndEarn(int[] nums) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int num : nums) {
            if (map.containsKey(num)) {
                Integer value = map.get(num);
                map.put(num, num + value);
            } else {
                map.put(num, num);
            }
        }
        List<Integer> orderList = map.keySet().stream().sorted().collect(Collectors.toList());
        int length = orderList.size();
        int[] dp = new int[length];
        dp[0] = map.get(orderList.get(0));
        for (int i = 1; i < length; i++) {
            Integer item = orderList.get(i);
            if (!map.containsKey(item - 1)) {
                dp[i] = dp[i - 1] + map.get(item);
            } else {
                int val = i - 2 >= 0 ? dp[i - 2] : 0;
                Integer tmp = map.get(orderList.get(i)) + val;
                int max = Math.max(tmp, dp[i - 1]);
                dp[i] = max;
            }
        }
        return dp[length - 1];
    }
}
